/*
 * Created by JFormDesigner on Wed Jun 21 15:33:55 CST 2023
 */

package org.scct.chenhui.frames;

import org.scct.chenhui.dao.UserDao;
import org.scct.chenhui.model.UserBean;
import org.scct.chenhui.utils.ActionDo;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.List;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author Administrator
 * 局部变量和实例变量
 */
public class PnlUserManager extends JPanel {

    private DefaultTableModel model;

    public PnlUserManager() {
        initComponents();
        initData();
    }

    private void initData() {
        //1.创建一个表格的数据模型  添加标题行  ctrl+alt+f
        model = new DefaultTableModel();
        //2. 添加标题行
        String []cols={"编号","用户名","密码","性别","电话"};
        for (String col : cols) {
            model.addColumn(col);
        }
        //3.将数据模型设置到表格控件上
        table1.setModel(model);

        getTableData();

//        添加按钮对的点击
         btnAdd.addActionListener(new AbstractAction() {
             @Override
             public void actionPerformed(ActionEvent e) {
                 DlgAddUser dlgAddUser = new DlgAddUser(null,new ActionDo() {
                     @Override
                     public void afterdo(Object obj) {
                         getTableData();
                     }
                 });
//                 设置是否模态窗口
                 dlgAddUser.setModal(true);
                 dlgAddUser.setVisible(true);

             }
         });

         btnUpdate.addActionListener(new AbstractAction() {
             @Override
             public void actionPerformed(ActionEvent e) {
//                 如果一行都没选
                 if(table1.getSelectedRowCount()==0)
                 {
                     JOptionPane.showMessageDialog(null,"请选择需要修改的用户");
                     return;
                 }
                 int id = Integer.parseInt(table1.getValueAt(table1.getSelectedRow(), 0).toString());

                 DlgUpdateUser dlgUpdateUser = new DlgUpdateUser(null, id, new ActionDo() {
                     @Override
                     public void afterdo(Object obj) {
                         getTableData();
                     }
                 });
                 dlgUpdateUser.setModal(true);
                 dlgUpdateUser.setVisible(true);

             }
         });

//         table1.setSelectionMode(SingleSelectionModel);

    }
    //读取数据库，刷新表格内容
    private void getTableData()
    {
        model.setRowCount(0);

        UserDao userDao = new UserDao();
        List<UserBean> users = userDao.getAllUsers();
        for (UserBean user : users) {
            Object[] obj={user.getId(),user.getUsername(),user.getPassword(),user.getSex(),user.getPhone()};
            model.addRow(obj);        }

    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - brightch
        panel1 = new JPanel();
        btnAdd = new JButton();
        btnUpdate = new JButton();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();

        //======== this ========
        setBorder(new javax.swing.border.CompoundBorder(new javax.swing.border.TitledBorder(new javax.swing
        .border.EmptyBorder(0,0,0,0), "JF\u006frmDes\u0069gner \u0045valua\u0074ion",javax.swing.border.TitledBorder
        .CENTER,javax.swing.border.TitledBorder.BOTTOM,new java.awt.Font("D\u0069alog",java.
        awt.Font.BOLD,12),java.awt.Color.red), getBorder()))
        ; addPropertyChangeListener(new java.beans.PropertyChangeListener(){@Override public void propertyChange(java.beans.PropertyChangeEvent e
        ){if("\u0062order".equals(e.getPropertyName()))throw new RuntimeException();}})
        ;
        setLayout(new BorderLayout());

        //======== panel1 ========
        {
            panel1.setLayout(new FlowLayout(FlowLayout.RIGHT));

            //---- btnAdd ----
            btnAdd.setText("\u65b0\u589e");
            panel1.add(btnAdd);

            //---- btnUpdate ----
            btnUpdate.setText("\u4fee\u6539");
            panel1.add(btnUpdate);
        }
        add(panel1, BorderLayout.NORTH);

        //======== scrollPane1 ========
        {
            scrollPane1.setViewportView(table1);
        }
        add(scrollPane1, BorderLayout.CENTER);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - brightch
    private JPanel panel1;
    private JButton btnAdd;
    private JButton btnUpdate;
    private JScrollPane scrollPane1;
    private JTable table1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
